package com.bjsxt.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Controller
public class AliPayController {

    // 应用标识
    private final String APP_ID = "2016101900725971";
    // 应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7nfrTiBMOno0XhSI8D2l9q4awC32fxCfvuoNaiaJ3KjbSi0P6mEkC8IxUcuK+I7TCD/3F24o6cmPSabb4Z2HauszUc7lP/X650IkNIZRjbchnO/Va4XKn9gSgnq3x/29Wfyg0yRQMPAdlktib1T2/SP15bBkqES8x8y6YmfUjvjD2rqsEC8eXdUGvdnUWPkl0+gU1kwg5OmzGgNao5ogVlb2VNXHs+94ixfZKPupeSfUmmhpFQ23c+IvBsX5U1mI67ahi/OFd9vGwvyShTmG/SaP5tQbX6H9IE1jS4wEbq0WGpjinemXmcCaJ0/dT8sFzRJnJ4kQCkdgk0LveTx6DAgMBAAECggEBAJGSWGX8tNypBMx1DkUPqEhL6Gt73vMx3dLSIs0C8b4IURU33Xz4392aCygTI/9n8P6KLdtxJmWHEBDY2ZEPlguC2R8W03hPNh6ywUQhA+PJ8K65BPyI5M+RwbFmrYyRJzAz5VxfkGCVyx9yuwLeh9R9q76ksznCr53QFB1vh5c8yVLdUgyUEr//0L+m5Z/XlWmMU5QMpMa/L8d/meuqeOcIOcbmrV/C15KehRi8BjhJUSHVXbpK+6WPxhJLhAUWdly1YMq34CuBit99BkkJA3OL4BAIIA6Vy/uLZddiyurHQ7KIjP7GDETjGbA1DEyWlA3AOl1UnWptMAu3bbJXBEECgYEA/U4/xHfGiGKIupSmGwsxa9lJ9r94+lFls4hPCzkL+oLrswlix8hzamVoiYq2xv+9H0nJ+ml9q0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n0vvi9tUoHi0PCJJoEEFIPpM4vgMQLVdV7jyZ/ztnvvOQAO/p4DFXKGW15mcI7wSHRpjp4kKxW58xc6R70PA1dYp92EDNe3ufZA/uYig1RbNwdj7WBHf0UGZlvbPaaN+k9/ftBrPX1ywIUVooMj7prWUKg9j8FZDCXnxDRuBCpS7TcSzvR77ABwOxeHMn2FOJwIDAQAB";
    // 沙箱路径接口，正是路径应为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    // 参数返回格式
    private final String FORMAT = "JSON";
    // 签名方式
    private final String SIGN_TYPE = "RSA2";
    // 支付宝异步通知路径，付款完毕后会异步调用本项目的方法，必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyURL";
    // 支付宝同步通知路径，也就是当付款完毕后跳转本项目的页面，可以不是公网地址
    private final String RETURN_URL = "http://127.0.0.1/returnURL";


    @RequestMapping("alipay")
    public void alipay(HttpServletResponse httpResponse) throws IOException {
        // 实例化客户端,填入所需参数
        // 网关地址 应用标识 应用私钥 传输格式 编码格式 支付宝公钥 签名类型
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Integer.toString(100);
        //订单名称，必填
        String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
